package com.webserver.core;

import com.webserver.controller.UserController;
import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;

import java.io.File;
import java.io.IOException;

/**
 * 用于处理请求
 */
public class DispatcherServlet {
    public void service(HttpRequest httpRequest, HttpResponse response) {
        String patch = httpRequest.getRequestURI();
        //首先判断该请求是否为请求一个业务
        if ("/myweb/regUser".equals(patch)) {
            //处理注册
            UserController controller=new UserController();
            controller.reg(httpRequest,response);
        } else if("/myweb/loginUser".equals(patch)){
            UserController controller=new UserController();
            controller.login(httpRequest,response);

        } else {
            File file = new File("./webapps" + patch);
            if (file.exists() && file.isFile()) {
                //正确情况
                response.setEntity(file);

            } else {
                response.setStatusCode(404);
                response.setStatusReason("NotFound");
                file = new File("./webapps/root/404.html");
                response.setEntity(file);
            }

        }
        //该响应头是告知浏览器服务端是谁
        response.putHeader("Server", "WebServer");
    }
}
